﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 字符串parse与手动转换性能比较 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Action p2 = 手动转换;
        //手动转换速度更快
        Action p1 = Parse转换;
        Action p3 = 循环时间;
        Action p4= Convert转换;
        Response.Write(string.Format("{0} 运行 {1} 万次,用时{2}毫秒<br/>", "类库转换", count / 10000.0, p1.StopwatchRun()));
        Response.Write(string.Format("{0} 运行 {1} 万次,用时{2}毫秒<br/>", "手动转换", count / 10000.0, p2.StopwatchRun()));
        Response.Write(string.Format("{0} 运行 {1} 万次,用时{2}毫秒<br/>", "循环时间", count / 10000.0, p3.StopwatchRun()));
        Response.Write(string.Format("{0} 运行 {1} 万次,用时{2}毫秒<br/>", "基类转换", count / 10000.0, p4.StopwatchRun()));



    }

    public void 循环时间()
    {
        for (int i = 0; i < count; i++)
        {
             
        }
    }

    int count = 5 * 100 * 10000;
    public void Parse转换()
    {
        for (int i = 0; i < count; i++)
        {
            int.Parse(num);
        }
    }
    public void Convert转换()
    {
        for (int i = 0; i < count; i++)
        {
            Convert.ToInt32(num);
        }
    }


    //string num = "-1234567890";
    string num = "-1234567890";
    public void 手动转换()
    {

        for (int i = 0; i < count; i++)
        {
            IntParse(num);
        }




        //Response.Write();

    }

    /// <summary>
    /// 自定义转换int
    /// </summary>
    /// <param name="numString">字符串</param>
    /// <returns>返回转换后的int</returns>
    public int IntParse(string numString)
    {
        //当字符串长度小于4时 int.parse性能更好
        //if (numString.Length < 4) { return int.Parse(numString); }

        int result = 0;
        int tmp = 0;
        int index = numString.Length;
        int d = numString[0] == '-' ? -1 : 0;

        foreach (char c in numString)
        {
            switch (c)
            {
                case '0':
                    tmp = 0;
                    break;
                case '1':
                    tmp = 1;
                    break;
                case '2':
                    tmp = 2;
                    break;
                case '3':
                    tmp = 3;
                    break;
                case '4':
                    tmp = 4;
                    break;
                case '5':
                    tmp = 5;
                    break;
                case '6':
                    tmp = 6;
                    break;
                case '7':
                    tmp = 7;
                    break;
                case '8':
                    tmp = 8;
                    break;
                case '9':
                    tmp = 9;
                    break;
                case '-':
                    d = -1;
                    index--;
                    continue;
                    break;
                default:
                    throw new NotImplementedException("不支持的数字类型");
                    break;
            }
            switch (index)
            {
                case 1:
                    result += tmp;
                    break;
                case 2:
                    result += tmp * 10;
                    break;
                case 3:
                    result += tmp * 100;
                    break;
                case 4:
                    result += tmp * 1000;
                    break;
                case 5:
                    result += tmp * 10000;
                    break;
                case 6:
                    result += tmp * 100000;
                    break;
                case 7:
                    result += tmp * 1000000;
                    break;
                case 8:
                    result += tmp * 10000000;
                    break;
                case 9:
                    result += tmp * 100000000;
                    break;
                case 10:
                    result += tmp * 1000000000;
                    break;
                //default:
                //    throw new NotImplementedException("int类型无法表示" + (long)(tmp * Math.Pow(10, index)));
                //    break;
            }

            index--;

        }
        if (d < 0)
        {
            result = 0 - result;
        }

        return result;

    }
}